//
//  SearchTableView.m
//  MedicineBox
//
//  Created by dllo on 15/9/19.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "SearchTableView.h"
#import "CustomSearchCell.h"
#import "AsyncConnection.h"
#import "DKNightVersion.h"
@interface SearchTableView ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) CustomSearchCell *cell;
@property (nonatomic, retain) NSMutableArray *contentArray;
@property (nonatomic, retain) NSMutableArray *tableArray;
@end
@implementation SearchTableView

- (void)dealloc
{
    
    [_cell release];
    [_contentArray release];
    [_tableArray release];
    [super dealloc];
    
}
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        
        self.tableArray = [NSMutableArray array];
        self.contentArray = [NSMutableArray array];
        
        self.delegate = self;
        self.dataSource = self;
        
        
    }
    return self;
}

- (void)getArray:(NSArray *)array title:(NSString *)title
{

    [_tableArray removeAllObjects];
    [self.tableArray addObjectsFromArray:array];
   
    [self reloadData];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellIden = @"";
    self.cell = [tableView dequeueReusableCellWithIdentifier:cellIden];

    if (_cell == nil) {
        
        _cell = [[CustomSearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIden ];
        
        
    }
    _cell.nightBackgroundColor = UIColorFromRGB(0x343434);
    if (_tableArray.count != 0) {
        
        NSDictionary *dic = [self.tableArray objectAtIndex:indexPath.section];
        NSArray *array = [dic objectForKey:@"thridCategory"];
        if (array.count % 3 == 0) {
       [_cell getSearContentWithDic:[array objectAtIndex:indexPath.row * 3] dic2:[array objectAtIndex:indexPath.row * 3 + 1 ] dic3:[array objectAtIndex:indexPath.row * 3 + 2]];
            
        } else if (array.count % 3 != 0) {
        
            if (array.count > 3) {
                
                if (array.count % 3 == 1) {
                    
                    if (indexPath.row * 3 < array.count - 1) {
                        
                        [_cell getSearContentWithDic:[array objectAtIndex:indexPath.row * 3] dic2:[array objectAtIndex:indexPath.row * 3 + 1 ] dic3:[array objectAtIndex:indexPath.row * 3 + 2]];
                        
                    } else {
                    
                        [_cell getSearContentWithDic:[array objectAtIndex:indexPath.row * 3] dic2:nil dic3:nil];
                    
                    }
                    
                } else if (array.count % 3 == 2) {
                    
                    if (indexPath.row * 3 < array.count - 2) {
                        
                        [_cell getSearContentWithDic:[array objectAtIndex:indexPath.row * 3] dic2:[array objectAtIndex:indexPath.row * 3 + 1 ] dic3:[array objectAtIndex:indexPath.row * 3 + 2]];
                        
                    } else {
                        
                        [_cell getSearContentWithDic:[array objectAtIndex:indexPath.row * 3] dic2:[array objectAtIndex:indexPath.row * 3 + 1 ] dic3:nil];
                        
                    }

                    
                }
                
            }
        
        }
        
    }
    _cell.target = self;
    _cell.action = @selector(tapAction:);
    return _cell;
}

- (void)tapAction:(NSDictionary *)dic
{

    [(NSObject *)self.target performSelector:self.action withObject:dic];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 90;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

   
    NSDictionary *dic = [self.tableArray objectAtIndex:section];
    NSArray *array = [dic objectForKey:@"thridCategory"];

    if (array.count % 3 == 0) {
        
        return array.count / 3;
        
    } if (array.count % 3 != 0) {
        
        return array.count / 3 + 1;
        
    } else {
    
        return 0;
    
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    
    return 30;

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{

    NSDictionary *dic = [self.tableArray objectAtIndex:section];
    return [dic objectForKey:@"name"];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return self.tableArray.count;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

@end
